# -*- coding: utf-8 -*-
'''
@文件    :notifyFunc.py
@说明    :
@时间    :2024/02/23 11:40:13
@作者    :浪晋
@版本    :1.0
'''
import requests
from config import flaskConfig
from loguru import logger




def qiyeWeixin(msgtype,media_id=None,content=None):
    """
    给企业微信发送通知
    """
    url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={flaskConfig.robotConfig['qyweixin']}"
    method = "post"
    if msgtype == "file":
        data = {
            "msgtype": "file",
            "file": {
                "media_id": media_id
            }
        }
    else:
        data = {
        "msgtype": "text",
        "text": {
            "content": content
        }
        }
    r = requests.request(method,url,json=data)
    logger.info(f"企业微信发送结果:{r.text}")



def uploadToQW(filePath):
    """
    上传附件到企业微信中
    """
    url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={flaskConfig.robotConfig['qyweixin']}&type=file"
    method = "post"
    data = {"media":open(filePath,"rb")}
    r = requests.request(method,url,files=data)
    logger.info(f"企业微信发送结果:{r.text}")
    # {"errcode":0,"errmsg":"ok","type":"file","media_id":"3e9n94LAUCtwasZp-m8WEQEtmJ4JhW8PpSFi5qB3MfmUhFoKyREhMzGpnSSX-xjsT","created_at":"1676710555"}
    return r.json()["media_id"]


def dingding(content,msgtype="markdown"):
    """
    @功能    :钉钉发送消息的机器人
    @参数    :
    @返回值  :
    @时间    :2023/05/20 17:46:16
    @作者    :浪晋
    @版本    :1.0
    """
    method = "post"
    url = f"https://oapi.dingtalk.com/robot/send?access_token={flaskConfig.robotConfig['dingding']}"
    if msgtype == "text":
        data = {
            "msgtype": "text",
            "text": {"content":content}
        }
    else:
        data = {
            "msgtype": "markdown",
            "markdown": {"title":"进件通知","text":content}
        }
    r = requests.request(method=method,url=url,json=data)
    logger.info(f"钉钉发送结果:{r.text}")
